package com.david.zhufengfm.utils;

import android.util.Log;

import com.david.zhufengfm.Constants;
import com.david.zhufengfm.entity.DiscoverCategory;
import com.david.zhufengfm.entity.album.AlbumDetail;
import com.david.zhufengfm.entity.album.AlbumDetailInfo;
import com.david.zhufengfm.entity.album.AlbumTrackDetail;
import com.david.zhufengfm.entity.album.PlayTrack;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendAlbums;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendColumns;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendFocus;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendItem;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendSpecial;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by David on 2015/10/20.
 * qq:978944820
 */
public class EntityParseUtil {

    public static List<DiscoverCategory> parseDiscoverCategory(JSONObject json) {
        List<DiscoverCategory> ret = null;
        if (json != null) {
            try {
                int code = json.getInt("ret");
                //获取数据成功
                if (code == Constants.TASK_RESULT_OK) {
                    ret = new LinkedList<>();
                    JSONArray array = json.getJSONArray("list");
                    int len = array.length();
                    if (len > 0) {
                        for (int i = 0; i < len; i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            DiscoverCategory category = new DiscoverCategory();
                            //利用实体类内部实现JSON解析，外部的代码，就是
                            //调用方法即可
                            category.parseJson(jsonObject);
                            ret.add(category);
                        }

                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return ret;
    }

    /**
     * 解析"发现"模块中，推荐栏目的数据结构
     *
     * @param jsonObject
     * @return
     */
    public static List<DiscoverRecommendItem> parseDiscoverRecommend(JSONObject jsonObject) {
        List<DiscoverRecommendItem> ret = null;
        if (jsonObject != null) {
            try {
                int code = jsonObject.getInt("ret");
                if (code == Constants.TASK_RESULT_OK) {
                    ret = new LinkedList<DiscoverRecommendItem>();

                    //小编推荐，内容的解析
                    JSONObject object =
                            jsonObject.getJSONObject("editorRecommendAlbums");
                    DiscoverRecommendAlbums editorRecommend =
                            new DiscoverRecommendAlbums();
                    editorRecommend.parseJson(object);
                    ret.add(editorRecommend);
                    //--------------------------------------------------------------------
                    //解析发现新奇
                    JSONObject columnsObject=jsonObject.getJSONObject("discoveryColumns");
                    DiscoverRecommendColumns recommendColumns=new DiscoverRecommendColumns();
                    recommendColumns.parseJSON(columnsObject);
                    ret.add(recommendColumns);
                    Log.i("recommendColumns", "" + recommendColumns.getColumnsRecommends().size());
                    //----------------------------------------------------------------------
                    //解析精品听单
                    JSONObject specialObject=jsonObject.getJSONObject("specialColumn");
                    DiscoverRecommendSpecial recommendSpecial=new DiscoverRecommendSpecial();
                    recommendSpecial.parseJSON(specialObject);
                    ret.add(recommendSpecial);


                    //--------------------------------------------------------
                    //解析热门推荐
                    JSONObject hotObject = jsonObject.getJSONObject("hotRecommends");
                    JSONArray jsonArray = hotObject.getJSONArray("list");
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject obj = jsonArray.getJSONObject(i);
                            DiscoverRecommendAlbums discoverRecommend = new DiscoverRecommendAlbums();
                            discoverRecommend.parseJson(obj);
                            ret.add(discoverRecommend);
                        }
                    }
                    //------------------------------------------------------------
                    //解析广告
                    JSONObject focus=jsonObject.getJSONObject("focusImages");
                    DiscoverRecommendFocus recommendFocus=new DiscoverRecommendFocus();
                    recommendFocus.parseJSON(focus);
                    ret.add(recommendFocus);
                    Log.i("recommendFocus", "" + recommendFocus.getFocusList().size());
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    /**
     * 解析专辑的详情以及里面的曲目
     * @param jsonObject
     * @return
     */
    public static AlbumDetail parseAlbumDetail(JSONObject jsonObject){
        AlbumDetail albumDetail=null;
        if (jsonObject != null) {
            try {
                JSONObject albumObject=jsonObject.getJSONObject("album");
                TypeToken<AlbumDetailInfo>infoToken=new TypeToken<AlbumDetailInfo>(){};
                Gson gson=new Gson();
                albumDetail=new AlbumDetail();
                AlbumDetailInfo albumInfo = gson.fromJson(albumObject.toString(), infoToken.getType());
                albumDetail.setAlbumInfo(albumInfo);
                JSONObject tracksObject=jsonObject.getJSONObject("tracks");
                List<AlbumTrackDetail>albumTracks=new LinkedList<>();
                JSONArray tracksArr=tracksObject.getJSONArray("list");
                for (int i = 0; i < tracksArr.length(); i++) {
                    JSONObject trackObject=tracksArr.getJSONObject(i);
                    TypeToken<AlbumTrackDetail>trackToken=new TypeToken<AlbumTrackDetail>(){};
                    AlbumTrackDetail albumTrackDetail = gson.fromJson(trackObject.toString(), trackToken.getType());
                    albumTracks.add(albumTrackDetail);
                }
                albumDetail.setTracks(albumTracks);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return albumDetail;
    }
    public static List<PlayTrack> getPlayTracks(JSONObject jsonObject){
        ArrayList<PlayTrack>ret=null;

        if (jsonObject != null) {
            try {
                JSONArray jsonArray=jsonObject.getJSONArray("data");
                TypeToken<List<PlayTrack>> token=new TypeToken<List<PlayTrack>>(){};
                ret=new Gson().fromJson(jsonArray.toString(),token.getType());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}

